home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / os.py < prev    next >
Text File  |  1998-06-24  |  4KB  |  144 lines

  1. # os.py -- either amiga, mac, dos or posix depending on what system we're on.
  2.  
  3. # This exports:
  4. # - all functions from either posix or mac or amiga, e.g., os.unlink, os.stat, etc.
  5. # - os.path is either module posixpath or macpath or amigapath
  6. # - os.name is either 'posix' or 'mac' or 'amiga'
  7. # - os.curdir is a string representing the current directory ('.' or ':' or '')
  8. # - os.pardir is a string representing the parent directory ('..' or '::' or '/')
  9. # - os.sep is the (or a most common) pathname separator ('/' or ':')
  10. # - os.pathsep is the component separator used in $PATH etc
  11. # - os.defpath is the default search path for executables
  12.  
  13. # Programs that import and use 'os' stand a better chance of being
  14. # portable between different platforms.  Of course, they must then
  15. # only use functions that are defined by all platforms (e.g., unlink
  16. # and opendir), and leave all pathname manipulation to os.path
  17. # (e.g., split and join).
  18.  
  19. _osindex = {
  20.       'amiga': ('', '/', '/', ';', 'C:'),
  21.       'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'),
  22.       'dos':   ('.', '..', '\\', ';', '.;C:\\bin'),
  23.       'nt':    ('.', '..', '\\', ';', '.;C:\\bin'),
  24.       'mac':   (':', '::', ':', '\n', ':'),
  25. }
  26.  
  27. # For freeze.py script:
  28. if 0:
  29.     import posix
  30.     import posixpath
  31.  
  32. import sys
  33. for name in _osindex.keys():
  34.     if name in sys.builtin_module_names:
  35.         curdir, pardir, sep, pathsep, defpath = _osindex[name]
  36.         exec 'from %s import *' % name
  37.         exec 'import %spath' % name
  38.         exec 'path = %spath' % name
  39.         exec 'del %spath' % name
  40.         try:
  41.             exec 'from %s import _exit' % name
  42.         except ImportError:
  43.             pass
  44.         try:
  45.             environ
  46.         except:
  47.             environ = {} # Make sure os.environ exists, at least
  48.         break
  49. else:
  50.     del name
  51.     raise ImportError, 'no os specific module found'
  52.  
  53. def execl(file, *args):
  54.     execv(file, args)
  55.  
  56. def execle(file, *args):
  57.     env = args[-1]
  58.     execve(file, args[:-1], env)
  59.  
  60. def execlp(file, *args):
  61.     execvp(file, args)
  62.  
  63. def execlpe(file, *args):
  64.     env = args[-1]
  65.     execvpe(file, args[:-1], env)
  66.  
  67. def execvp(file, args):
  68.     _execvpe(file, args)
  69.  
  70. def execvpe(file, args, env):
  71.     _execvpe(file, args, env)
  72.  
  73. _notfound = None
  74. def _execvpe(file, args, env = None):
  75.     if env:
  76.         func = execve
  77.         argrest = (args, env)
  78.     else:
  79.         func = execv
  80.         argrest = (args,)
  81.         env = environ
  82.     global _notfound
  83.     head, tail = path.split(file)
  84.     if head:
  85.         apply(func, (file,) + argrest)
  86.         return
  87.     if env.has_key('PATH'):
  88.         envpath = env['PATH']
  89.     else:
  90.         envpath = defpath
  91.     import string
  92.     PATH = string.splitfields(envpath, pathsep)
  93.     if not _notfound:
  94.         import tempfile
  95.         # Exec a file that is guaranteed not to exist
  96.         try: execv(tempfile.mktemp(), ())
  97.         except error, _notfound: pass
  98.     exc, arg = error, _notfound
  99.     for dir in PATH:
  100.         fullname = path.join(dir, file)
  101.         try:
  102.             apply(func, (fullname,) + argrest)
  103.         except error, (errno, msg):
  104.             if errno != arg[0]:
  105.                 exc, arg = error, (errno, msg)
  106.     raise exc, arg
  107.  
  108. # Provide listdir for Windows NT that doesn't have it built in
  109. if name == 'nt':
  110.     try:
  111.         _tmp = listdir
  112.         del _tmp
  113.     except NameError:
  114.         def listdir(name):
  115.             if path.ismount(name):
  116.                 list = ['.']
  117.             else:
  118.                 list = ['.', '..']
  119.             f = popen('dir/l/b ' + name, 'r')
  120.             line = f.readline()
  121.             while line:
  122.                 list.append(line[:-1])
  123.                 line = f.readline()
  124.             return list
  125.  
  126.  
  127. # Change environ to automatically call putenv() if it exists
  128. try:
  129.     _putenv = putenv
  130. except NameError:
  131.     _putenv = None
  132. if _putenv:
  133.     import UserDict
  134.  
  135.     class _Environ(UserDict.UserDict):
  136.         def __init__(self, environ):
  137.             UserDict.UserDict.__init__(self)
  138.             self.data = environ
  139.         def __setitem__(self, key, item):
  140.             putenv(key, item)
  141.             self.data[key] = item
  142.  
  143.     environ = _Environ(environ)
  144.